/**************************************************************************
 *
 *        Copyright (c) 2007-2008 by Sunplus mMedia Inc., Ltd.
 *
 *  This software is copyrighted by and is the property of Sunplus
 *  mMedia Inc., Ltd. All rights are reserved by Sunplus mMedia
 *  Inc., Ltd. This software may only be used in accordance with the
 *  corresponding license agreement. Any unauthorized use, duplication,
 *  distribution, or disclosure of this software is expressly forbidden.
 *
 *  This Copyright notice MUST not be removed or modified without prior
 *  written consent of Sunplus mMedia Inc., Ltd.
 *
 *  Sunplus mMedia Inc., Ltd. reserves the right to modify this
 *  software without notice.
 *
 *  Sunplus mMedia Inc., Ltd.
 *  19-1, Innovation First Road, Science-Based Industrial Park,
 *  Hsin-Chu, Taiwan, R.O.C.
 *
 **************************************************************************/
/**
 * @filename 	hal_front_common.c
 * @brief 	The common file for front driver
 * @author 	cs.lin
 * @since 	2008-07-17
 * @date 	2008-10-29
 */

/**************************************************************************
 *                            H E A D E R   F I L E S
 **************************************************************************/
#include "general.h"
#include "front_api.h"
#include "sensor_cdsp.h"
#include "aaa_api.h"

/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S
 **************************************************************************/
extern xdata UINT8 G_GAIN;
extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_AEReady;
extern xdata UINT8 G_AESetting;

#if 0  // mattwang remove.
void
HalFrontAeSet(
	UINT16 idx,
	UINT8 opt
)
{
	UINT32 shutter;
	UINT16 gain;
	frontPrevCapabDesc_t *pcap;


	frontCapabilityGet(0, &pcap, NULL);
	if ( opt == 60 ) {
		shutter = pcap->ae60hzTbl[ idx ].shutter;
		gain = pcap->ae60hzTbl[ idx ].gain;
	}
	else {
		shutter = pcap->ae50hzTbl[ idx ].shutter;
		gain = pcap->ae50hzTbl[ idx ].gain;
	}

	G_AEReady = 1;
	G_SHUTTER = shutter;
	G_GAIN = gain;

	XBYTE[0x21EC] = XBYTE[0x21EC] | 0x04;//AE,AWB interrupt & CDSP Eof
	HAL_FrontIntrEnable(1, 0x41);

	while( !( G_AEReady == 0 && G_AESetting == 0 ) );

	XBYTE[0x21EC] = XBYTE[0x21EC] & 0xFB;//AE window done intr disable

	return;
}
#endif

void
HalFrontAeGet(
	UINT16 idx,
	UINT8 opt,
	UINT32 *shutter,
	UINT16 *gain
)
{
	frontPrevCapabDesc_t *pcap;


	frontCapabilityGet(0, &pcap, NULL);
	if ( opt == 60 ) {
		*shutter = pcap->ae60hzTbl[ idx ].shutter;
		*gain = pcap->ae60hzTbl[ idx ].gain;
	}
	else {
		*shutter = pcap->ae50hzTbl[ idx ].shutter;
		*gain = pcap->ae50hzTbl[ idx ].gain;
	}

	return;
}

void
HalFrontUSWait(
	UINT32 time_wait
)
{
	UINT8 sts;

	ENTER_CRITICAL(sts);

	XBYTE[0x2074] = 0x02;

	time_wait = (UINT32) 0xFFFFFFFF - time_wait;

	XBYTE[0x2070] = M_LoByteOfDword(time_wait);
	XBYTE[0x2071] = M_MidLoByteOfDword(time_wait);
	XBYTE[0x2072] = M_MidHiByteOfDword(time_wait);

	XBYTE[0x2074] = 0x01;

	while((XBYTE[0x2070] != 0x00) || (XBYTE[0x2071] != 0x00) || (XBYTE[0x2072] != 0));

	EXIT_CRITICAL(sts);
}

